package org.example.consumer.controller;

import org.example.consumer.component.ServiceURL;
import org.example.pojo.ItripUser;
import org.example.utils.ResultTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/itripUser")
public class AuthController {
    private static final String REQUEST_URL = "itripUser/";
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private ServiceURL serviceURL;

    @GetMapping(value = "/getCode")
    public Object getCode(String phone,Integer page){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("phone", phone);
        paramMap.put("page", page);
        return restTemplate.getForObject(
                serviceURL.getAuthUrl()+REQUEST_URL+"getCode",
                ResultTemplate.class,paramMap);
    }

    @PostMapping(value = "/checkRegisterCode")
    public Object checkRegisterCode(String phone,String code,Integer page) {
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
        paramMap.add("phone", phone);
        paramMap.add("page", page);
        paramMap.add("code", code);
//        ?phone="+phone+"&code="+code+"&page="+page
        return restTemplate.postForObject(
                serviceURL.getAuthUrl()+REQUEST_URL+"checkRegisterCode",paramMap,ResultTemplate.class);
    }

    @PostMapping(value = "/register")
    public Object register(@RequestBody ItripUser itripUser){
        return restTemplate.postForObject(serviceURL.getAuthUrl()+REQUEST_URL+"register",itripUser,ResultTemplate.class);
    }

    @PostMapping(value = "/login")
    public Object login(@RequestParam String phone,
                        @RequestParam(name = "code",required = false) String code,
                        @RequestParam(name = "password",required = false) String password,
                        @RequestParam(required = false) Integer page){
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
        paramMap.add("phone",phone);
        paramMap.add("code",code);
        paramMap.add("page",page);
        paramMap.add("password",password);
        return restTemplate.postForObject(serviceURL.getAuthUrl()+REQUEST_URL+"login",paramMap,ResultTemplate.class);
    }
}
